<template>
  <j-modal :title="pTitle" :width="1200" :visible="visible" :confirmLoading="loading" switchFullscreen
    @cancel="handleCancel" @ok="handleOk">
    <a-form-model ref="ruleForm" :model="form" :rules="rules" :label-col="{ span: 6 }" :wrapper-col="{ span: 18 }">
      <a-row>
        <a-col :span="15">
          <a-form-model-item label="人员姓名" required prop="personId">
            <a-select style="width:100%;" v-model="form.personId" allowClear :disabled="isDisabled">
              <a-select-option v-for="(item, index) in personList" :key="index" :value="item.id"
                @click.native="nameChange(item)">{{ item.realName }}</a-select-option>
            </a-select>
          </a-form-model-item>
          <a-form-model-item label="身份证号" prop="identification">
            <a-input v-model="form.identification" placeholder="请输入" :disabled="isDisabled" />
          </a-form-model-item>
          <a-form-model-item label="性别" prop="sex">
            <a-select v-model="form.sex" placeholder="请选择" :disabled="isDisabled">
              <a-select-option value="1">男</a-select-option>
              <a-select-option value="2">女</a-select-option>
            </a-select>
          </a-form-model-item>
          <a-form-model-item label="家庭地址" prop="address">
            <a-input v-model="form.address" placeholder="请输入" :disabled="isDisabled" />
          </a-form-model-item>
          <a-form-model-item label="评估类型" prop="assessmentType">
            <a-select v-model="form.assessmentType" placeholder="请选择" :disabled="isDisabled">
              <a-select-option value="1">季度评估</a-select-option>
              <a-select-option value="2">年度评估</a-select-option>
            </a-select>
          </a-form-model-item>
          <a-form-model-item label="评估时段" prop="assessmentTime">
            <a-range-picker v-model="form.assessmentTime" style="width:100%;" valueFormat="YYYY-MM-DD"
              @change="onTimeChange" :disabled="isDisabled" allowClear />
          </a-form-model-item>
        </a-col>
      </a-row>
      <p class="pt pl">管控情况</p>
      <div>
        <!-- <TotalInfoItem ref="infoItemId" /> -->
        <control-service :jsonParam="jsonParam" ref="infoItemId" />
      </div>
      <p class="pt pl" style="margin-top: 24px;">评估情况</p>
      <a-row>
        <a-col :span="10" class="item1">
          <a-form-model-item label="综合评估" prop="comEvaluation">
            <a-textarea v-model="form.comEvaluation" placeholder="请输入至少五个字符" :auto-size="{ minRows: 3, maxRows: 5 }" />
          </a-form-model-item>
        </a-col>
      </a-row>
      <template v-if="isShow">
        <p class="pt1">逾期信息（必填） <a-tooltip placement="rightBottom"
            :overlayStyle="{ minWidth: `300px`, maxWidth: `400px` }">
            <template slot="title">
              <div>该检测已逾期，请填写以下逾期信息提交审批</div>
            </template>
            <a-icon type="question-circle" />
          </a-tooltip>
        </p>
        <a-row>
          <a-col :span="17">
            <a-form-model-item label="逾期原因" prop="reasonOverdue">
              <a-select v-model="form.reasonOverdue" placeholder="请选择">
                <a-select-option value="0">
                  个人原因
                </a-select-option>
                <a-select-option value="1">
                  其他
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="17">
            <a-form-model-item label="原因说明" prop="reasonDescription">
              <a-textarea v-model="form.reasonDescription" placeholder="请输入至少五个字符"
                :auto-size="{ minRows: 4, maxRows: 6 }" />
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="17">
            <a-form-model-item label="附件">
              <j-upload v-model="form.overdueFileId" :number="1"></j-upload>
              <p>支持扩展名：.jpg .png .pdf</p>
            </a-form-model-item>
          </a-col>
        </a-row>
      </template>
    </a-form-model>
  </j-modal>
</template>
<script>
// import TotalInfoItem from '@/views/sjsk/modules/TotalInfoItem'
import controlService from '@/views/sjsk/flow/examine/modules/controlService'
import * as totalEstimate from '@/api/sjsk/help/totalEstimate.js'
import { getDictItemsFromCache } from '@/api/api'
import * as personServiceApi from '@/api/sjsk/personService.js'
import {
  assessStatisticalToUrine, assessStatisticalToInterview,
  assessStatisticalToReport, assessStatisticalToOther, personSignCount
} from '@/api/sjsk/estimate/list/index'
import * as clinicmanageServiceApi from '@/api/sjsk/clinicmanageService.js'
export default {
  watch: {

  },
  props: ['pTitle'],
  name: 'AddModal',
  components: {
    controlService
    // TotalInfoItem
  },
  data() {
    return {
      visible: false,
      form: {
        personId: "",
        assessmentTime: undefined
      },
      isDisabled: true,
      rules: {
        personId: [{ required: true, message: '请选择' }],
        identification: [{ required: true, message: '请输入' }],
        sex: [{ required: true, message: '请选择' }],
        address: [{ required: true, message: '请输入' }],
        assessmentType: [{ required: true, message: '请选择' }],
        assessmentTime: [{ required: true, message: '请选择' }],
        comEvaluation: [{ required: true, message: '请输入' }, { validator: this.textVerification }],
        reasonOverdue: [{ required: true, message: '请选择' }],
        reasonDescription: [{ required: true, message: '请输入' }],
      },
      personList: [],
      isShow: false,
      loading: false,
      jsonParam: {},

      Urinedata: {},
      Interviewdata: {},
      Reportdata: {},
      Otherdata: {},
      Urinedata1: {},
      // SignCountdata: {},
      querydata: {},
      isResubmit: false,
      // entryStatus: ""
    }
  },
  async created() {

  },
  methods: {
    async show(record) {
      // console.log(record);
      this.getSJSK()
      this.visible = true
      this.form = {}
      this.form.id = record.id
      this.form.personId = Number(record.personId)
      this.form.name = record.name
      this.form.identification = record.identification
      this.form.sex = record.sex
      this.form.type = record.type
      this.form.address = record.currentAddress
      this.form.assessmentType = record.assessmentType.toString()
      this.form.assessmentStartTime = record.evaluationStartTime
      this.form.assessmentEndTime = record.evaluationEndTime
      this.form.assessmentTime = [record.evaluationStartTime, record.evaluationEndTime]
      // this.entryStatus = record.entryStatus
      console.log("record")
      console.log(record)
      this.jsonParam = {
        assessStartTime: record.evaluationStartTime,
        assessEndTime: record.evaluationEndTime,
        sjskPersonId: record.personId,
        identityCard: record.identification,
        userId: record.personId,
        signInEndTime: record.evaluationEndTime,
        signInStartTime: record.evaluationStartTime,
        caseNo: record.caseNo,
      }
      if (record.status == '4' || record.status == '5') { //重新提交
        this.isResubmit = true
        if (record.entryStatus == '1') { //已逾期
          this.isShow = true
          this.form.entryStatus = '1'
          // console.log("111111111111")
        }else{
          this.isShow = false
          // console.log("44444444444")
        }
      } else {
        // console.log("22222222222")
        this.isResubmit = false
        // console.log(this.jsonParam);
        if (record.entryStatus == '1') { //已逾期
          this.isShow = true
          this.form.entryStatus = '1'
          // console.log("22222222222")
        } else {
          this.isShow = false
          // console.log("33333333333333")
        }
      }
      // await this.qiandao(record)
      this.refreshTable()
    },

    //读取社戒社康人员
    getSJSK() {
      let pageFilter = {
        pageNo: 1,
        pageSize: 1000
      }
      personServiceApi
        .getPersonList(pageFilter)
        .then(res => {
          if (res.success) {
            this.personList = res.result.records
          }
        })
        .catch(e => {
          this.$message.warn('请求失败！')
        })
    },
    nameChange(e) {
      console.log(e)
    },
    onTimeChange(date) {
      this.$set(this.form, 'assessmentStartTime', date[0])
      this.$set(this.form, 'assessmentEndTime', date[1])
    },
    handleCancel() {
      this.visible = false
    },
    textVerification(rule, value, callback) {
      if (value && value.length < 5) {
        callback("请输入至少5个字符");
      } else {
        callback()
      }
    },
    handleOk() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          let str1 = this.$refs.infoItemId.getData1()
          let str2 = this.$refs.infoItemId.getData2()
          let str3 = this.$refs.infoItemId.getData3()
          let str4 = this.$refs.infoItemId.getData4()
          let str5 = this.$refs.infoItemId.getData5()
          let str6 = this.$refs.infoItemId.getData6()

          this.form.urineTest = str1
          this.form.hairDetection = str2
          this.form.regularInterviews = str3
          this.form.periodicReport = str4
          this.form.sign = str5
          this.form.otherInterventions = str6
          // if (this.entryStatus == '1') { //逾期的提交未补录
          //   if (this.form.assessmentType == '1') this.form.assessmentType = "3" //季度评估补录审批
          //   if (this.form.assessmentType == '2') this.form.assessmentType = "4" //年度评估补录审批
          // }
          console.log(this.form);
          this.loading = true
          let api = ""
          if (this.isResubmit) {
            api = totalEstimate.compEvaluationResubmit(this.form) //重新提交
          } else {
            api = totalEstimate.addCompEvaluation(this.form) //添加，补录
          }
          console.log(api);
          api.then(res => {
            if (res.success) {
              this.$message.success(res.message)
              this.$emit('ok');
              this.visible = false
              this.loading = false
            } else {
              this.loading = false
              this.$message.warn(res.message)
            }
          })
            .catch(e => {
              this.loading = false
              this.$message.warn('请求失败！')
            })
        }
      })
    },
    async refreshTable() {
      this.loading = true
      // 尿液检测
      // let params = Object.assign({}, this.querydata, { type: 1 })
      await assessStatisticalToUrine({ ...this.jsonParam, type: 1 }).then((res) => {
        if (res.success) {
          this.Urinedata = res.result ? res.result : {}
          this.loading = false
        } else {
          this.$message.warn(res.message)
          this.loading = false
        }
      })
        .catch((e) => {
          this.loading = false
          this.$message.warn('请求失败！')
        })

      // 毛发检测
      await assessStatisticalToUrine({ ...this.jsonParam, type: 3 }).then((res) => {
        if (res.success) {
          this.Urinedata1 = res.result ? res.result : {}
          // this.Controldata.Urinedata1 = res.result ? res.result : {}
          this.loading = false
        } else {
          this.$message.warn(res.message)
          this.loading = false
        }
      })
        .catch((e) => {
          this.loading = false
          this.$message.warn('请求失败！')
        })

      // 定期访谈
      await assessStatisticalToInterview(this.jsonParam).then((res) => {
        if (res.success) {
          this.Interviewdata = res.result ? res.result : {}
          this.loading = false
        } else {
          this.$message.warn(res.message)
          this.loading = false
        }
      })
        .catch((e) => {
          this.loading = false
          this.$message.warn('请求失败！')
        })

      // 定期报告
      await assessStatisticalToReport(this.jsonParam).then((res) => {
        if (res.success) {
          this.Reportdata = res.result ? res.result : {}
          this.loading = false
        } else {
          this.$message.warn(res.message)
          this.loading = false
        }
      })
        .catch((e) => {
          this.loading = false
          this.$message.warn('请求失败！')
        })

      // 其他介入情况
      await assessStatisticalToOther(this.jsonParam).then((res) => {
        if (res.success) {
          this.Otherdata = res.result ? res.result : {}
          this.loading = false
        } else {
          this.$message.warn(res.message)
          this.loading = false
        }
      })
        .catch((e) => {
          this.loading = false
          this.$message.warn('请求失败！')
        })

      // 签到
      console.log(this.jsonParam);
      await personSignCount(this.jsonParam).then((res) => {
        if (res.success) {
          this.SignCountdata = res.result ? res.result : {}
          this.loading = false
        } else {
          this.$message.warn(res.message)
          this.loading = false
        }
      })
        .catch((e) => {
          this.loading = false
          this.$message.warn('请求失败！')
        })

      this.jsonParam = {
        Urinedata: this.Urinedata,
        Interviewdata: this.Interviewdata,
        Reportdata: this.Reportdata,
        Otherdata: this.Otherdata,
        Urinedata1: this.Urinedata1,
        SignCountdata: this.SignCountdata,
      }
    },
  },
}
</script>
<style lang="less" scoped>
.pt {
  font-weight: bold;
}

.pl {
  padding-top: 20px;
  border-top: 1px dotted #ccc;
}

.pt1 {
  margin-top: 20px;
  // font-size: 16px;
  font-weight: bold;
  padding-top: 20px;
  border-top: 1px dotted #E9E9E9;
}

.ant-row {
  margin-left: 10px;
}

/deep/.ant-col-6 {
  width: 12%;
}

.item1 {
  width: 100%;
  position: relative;
  left: -2%;

  /deep/.ant-col-6 {
    width: 10.5%;
    // text-align: left;
  }

  /deep/.ant-col-16 {
    width: 70%;
    text-align: left;
  }
}
</style>
